package com.blogspot.ostas.oracle;

import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.BrowserCallback;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

public class App 
{
	static Logger logger = Logger.getLogger(App.class.getName());
	public static void main(String args[])
    {
        String configLocation = "applicationContext.xml";
		ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
		DefaultMessageListenerContainer msgListener 
			=  (DefaultMessageListenerContainer) context.getBean("jmsContainer");
		msgListener.destroy();
		
		JmsTemplate jmsTemplateOracle = (JmsTemplate) context.getBean("jmsTemplateOracle");
		
		jmsTemplateOracle.browse(new BrowserCallback<Object>() {
			@SuppressWarnings("unchecked")
			@Override
			public Object doInJms(Session session, QueueBrowser browser) throws JMSException {
				Enumeration<Message> e = browser.getEnumeration();
				Message message;
				while (e.hasMoreElements()) 
				{
					message = e.nextElement();
					final String messageBody = ((TextMessage) message).getText();
					logger.debug("message from queue : "+ messageBody);
				}
				return null;
			}
		});
    }
}
